package com.syn.synold4s.control;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.ListPopupWindow;

import com.gaoyi.basemodule.basebean.ProjectDto;
import com.syn.synold4s.adapter.PopupProjectAdapter;

import java.util.ArrayList;
import java.util.List;

public class ProjectPopupWindow extends ListPopupWindow {

    private PopupProjectAdapter mAdapter;
    private List<ProjectDto> mData;

    public void setData(List<ProjectDto> data, String projectId) {
        if (data == null) {
            return;
        }
        mData.clear();
        mData.addAll(data);
        for (int i = 0; i < mData.size(); i++) {
            mData.get(i).setSelect(mData.get(i).getId().equals(projectId));
        }
        mAdapter.notifyDataSetChanged();
    }

    public ProjectPopupWindow(@NonNull Context context) {
        super(context);
        init(context);
    }

    public ProjectPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ProjectPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public ProjectPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    @SuppressLint("RestrictedApi")
    private void init(Context context) {
        mData = new ArrayList<>();
        mAdapter = new PopupProjectAdapter(context, mData);
        setDropDownGravity(Gravity.START);
        setAdapter(mAdapter);
        setDropDownAlwaysVisible(false);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    }
}
